<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="110px">
      <el-form-item label="物料编码" prop="fnumber">
        <el-input
          v-model="queryParams.fnumber"
          placeholder="请输入物料编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料名称" prop="fname">
        <el-input
          v-model="queryParams.fname"
          placeholder="请输入物料名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卓正识别码" prop="fUnText">
        <el-input
          v-model="queryParams.fUnText"
          placeholder="请输入卓正识别码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料分组名称" prop="fmaterialgroupname">
        <el-input
          v-model="queryParams.fmaterialgroupname"
          placeholder="请输入物料分组名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料属性" prop="ferpclsid">
        <el-select v-model="queryParams.ferpclsid" placeholder="物料属性" size="small" clearable>
          <el-option label="外购" :value="1" />
          <el-option label="自制" :value="2" />
          <el-option label="委外" :value="3" />
          <el-option label="配置" :value="9" />
          <el-option label="资产" :value="10" />
          <el-option label="费用" :value="11" />
          <el-option label="虚拟" :value="5" />
          <el-option label="服务" :value="6" />
          <el-option label="产品系列" :value="13" />
        </el-select>
      </el-form-item>
      <el-form-item label="存货类别名称" prop="fcategoryname">
        <el-input
          v-model="queryParams.fcategoryname"
          placeholder="请输入存货类别名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="启用质保期管理" prop="fiskfperiod">
        <el-input
          v-model="queryParams.fiskfperiod"
          placeholder="请输入启用质保期管理"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="科室" prop="fPaezText">
        <el-input
          v-model="queryParams.fPaezText"
          placeholder="请输入科室"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="齿科专科分类" prop="fPaezText1">
        <el-input
          v-model="queryParams.fPaezText1"
          placeholder="请输入齿科专科分类"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否通用名称" prop="fUnComboname">
        <el-input
          v-model="queryParams.fUnComboname"
          placeholder="请输入是否通用名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="扫码系数" prop="fUnDecimal1">
        <el-input
          v-model="queryParams.fUnDecimal1"
          placeholder="请输入扫码系数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="适用医生" prop="fUnRemark2">
        <el-input
          v-model="queryParams.fUnRemark2"
          placeholder="请输入适用医生"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['k3:material:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['k3:material:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['k3:material:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['k3:material:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="materialList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="物料编码" align="center" prop="fnumber" />
      <el-table-column label="物料名称" align="center" prop="fname" />
      <el-table-column label="规格型号" align="center" prop="fspecification" />
      <el-table-column label="卓正识别码" align="center" prop="fUnText" />
      <el-table-column label="物料分组名称" align="center" prop="fmaterialgroupname" />
      <el-table-column label="物料属性" align="center" prop="ferpclsid">
        <template slot-scope="scope">
          <div>{{handleFerpclsid(scope.row)}}</div>
        </template>
      </el-table-column>
      <el-table-column label="存货类别名称" align="center" prop="fcategoryname" />
      <el-table-column label="启用质保期管理" align="center" prop="fiskfperiod" />
      <el-table-column label="科室" align="center" prop="fPaezText" />
      <el-table-column label="齿科专科分类" align="center" prop="fPaezText1" />
      <el-table-column label="是否通用名称" align="center" prop="fUnComboname" />
      <el-table-column label="扫码系数" align="center" prop="fUnDecimal1" />
      <el-table-column label="单位换算比例" align="center" prop="fUnDecimal2" />
      <el-table-column label="适用医生" align="center" prop="fUnRemark2" />
      <!-- <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['k3:material:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['k3:material:remove']"
          >删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="物料编码" prop="fnumber">
          <el-input v-model="form.fnumber" placeholder="请输入物料编码" />
        </el-form-item>
        <el-form-item label="物料名称" prop="fname">
          <el-input v-model="form.fname" placeholder="请输入物料名称" />
        </el-form-item>
        <el-form-item label="规格型号" prop="fspecification">
          <el-input v-model="form.fspecification" placeholder="请输入规格型号" />
        </el-form-item>
        <el-form-item label="卓正识别码" prop="fUnText">
          <el-input v-model="form.fUnText" placeholder="请输入卓正识别码" />
        </el-form-item>
        <el-form-item label="物料分组名称" prop="fmaterialgroupname">
          <el-input v-model="form.fmaterialgroupname" placeholder="请输入物料分组名称" />
        </el-form-item>
        <el-form-item label="物料属性" prop="ferpclsid">
          <el-input v-model="form.ferpclsid" placeholder="请输入物料属性" />
        </el-form-item>
        <el-form-item label="存货类别名称" prop="fcategoryname">
          <el-input v-model="form.fcategoryname" placeholder="请输入存货类别名称" />
        </el-form-item>
        <el-form-item label="启用质保期管理" prop="fiskfperiod">
          <el-input v-model="form.fiskfperiod" placeholder="请输入启用质保期管理" />
        </el-form-item>
        <el-form-item label="科室" prop="fPaezText">
          <el-input v-model="form.fPaezText" placeholder="请输入科室" />
        </el-form-item>
        <el-form-item label="齿科专科分类" prop="fPaezText1">
          <el-input v-model="form.fPaezText1" placeholder="请输入齿科专科分类" />
        </el-form-item>
        <el-form-item label="是否通用名称" prop="fUnComboname">
          <el-input v-model="form.fUnComboname" placeholder="请输入是否通用名称" />
        </el-form-item>
        <el-form-item label="条码" prop="fbarcode">
          <el-input v-model="form.fbarcode" placeholder="请输入条码" />
        </el-form-item>
        <el-form-item label="适用医生" prop="fUnRemark2">
          <el-input v-model="form.fUnRemark2" placeholder="请输入适用医生" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMaterial, getMaterial, delMaterial, addMaterial, updateMaterial, exportMaterial } from "@/api/k3/material";

export default {
  name: "Material",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 物料基础信息表格数据
      materialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fnumber: undefined,
        fname: undefined,
        fspecification: undefined,
        fdescription: undefined,
        fUnText: undefined,
        fdocumentstatus: undefined,
        fforbidstatus: undefined,
        fmaterialgroup: undefined,
        fmaterialgroupname: undefined,
        ferpclsid: undefined,
        fcategoryid: undefined,
        fcategoryname: undefined,
        fbaseunitid: undefined,
        fbaseunitname: undefined,
        fstoreunitid: undefined,
        fstoreunitname: undefined,
        fpurchaseunitid: undefined,
        fpurchaseunitname: undefined,
        fiskfperiod: undefined,
        fexpunit: undefined,
        fexpperiod: undefined,
        fPaezText: undefined,
        fPaezText11: undefined,
        fPaezText1: undefined,
        fconversioncoefficient: undefined,
        fUnCombo: undefined,
        fUnComboname: undefined,
        fUnText2: undefined,
        fUnText4: undefined,
        fUnDecimal1: undefined,
        fbarcode: undefined,
        fUnRemark1: undefined,
        fUnRemark2: undefined,
        fUnDecimal7: undefined,
        fUnDecimal6: undefined,
        fUnDecimal: undefined,
        fUnDecimal8: undefined,
        fUnText6: undefined,
        fUnText7: undefined,
        fUnText8: undefined,
        fUnText9: undefined,
        fUnDecimal2: undefined,
        fUnDecimal3: undefined,
        fUnDecimal4: undefined,
        fUnDecimal5: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fnumber: [
          { required: true, message: "物料编码不能为空", trigger: "blur" }
        ],
        fname: [
          { required: true, message: "物料名称不能为空", trigger: "blur" }
        ],
        fdocumentstatus: [
          { required: true, message: "数据状态不能为空", trigger: "blur" }
        ],
        fforbidstatus: [
          { required: true, message: "禁用状态不能为空", trigger: "blur" }
        ],
        fmaterialgroup: [
          { required: true, message: "物料分组不能为空", trigger: "blur" }
        ],
        ferpclsid: [
          { required: true, message: "物料属性不能为空", trigger: "blur" }
        ],
        fcategoryid: [
          { required: true, message: "存货类别不能为空", trigger: "blur" }
        ],
        fbaseunitid: [
          { required: true, message: "基本单位不能为空", trigger: "blur" }
        ],
        fstoreunitid: [
          { required: true, message: "库存单位不能为空", trigger: "blur" }
        ],
        fpurchaseunitid: [
          { required: true, message: "采购单位不能为空", trigger: "blur" }
        ],
        fiskfperiod: [
          { required: true, message: "启用质保期管理不能为空", trigger: "blur" }
        ],
        fexpunit: [
          { required: true, message: "质保期单位不能为空", trigger: "blur" }
        ],
        fexpperiod: [
          { required: true, message: "质保期不能为空", trigger: "blur" }
        ],
        fconversioncoefficient: [
          { required: true, message: "申领换算系数不能为空", trigger: "blur" }
        ],
        fUnCombo: [
          { required: true, message: "是否通用不能为空", trigger: "blur" }
        ],
        fUnDecimal1: [
          { required: true, message: "扫码系数不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询物料基础信息列表 */
    getList() {
      this.loading = true;
      listMaterial(this.queryParams).then(response => {
        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        fnumber: undefined,
        fname: undefined,
        fspecification: undefined,
        fdescription: undefined,
        fUnText: undefined,
        fdocumentstatus: "0",
        fforbidstatus: "0",
        fmaterialgroup: undefined,
        fmaterialgroupname: undefined,
        ferpclsid: undefined,
        fcategoryid: undefined,
        fcategoryname: undefined,
        fbaseunitid: undefined,
        fbaseunitname: undefined,
        fstoreunitid: undefined,
        fstoreunitname: undefined,
        fpurchaseunitid: undefined,
        fpurchaseunitname: undefined,
        fiskfperiod: undefined,
        fexpunit: undefined,
        fexpperiod: undefined,
        fPaezText: undefined,
        fPaezText11: undefined,
        fPaezText1: undefined,
        fconversioncoefficient: undefined,
        fUnCombo: undefined,
        fUnComboname: undefined,
        fUnText2: undefined,
        fUnText4: undefined,
        fUnDecimal1: undefined,
        fbarcode: undefined,
        fUnRemark1: undefined,
        fUnRemark2: undefined,
        fUnDecimal7: undefined,
        fUnDecimal6: undefined,
        fUnDecimal: undefined,
        fUnDecimal8: undefined,
        fUnText6: undefined,
        fUnText7: undefined,
        fUnText8: undefined,
        fUnText9: undefined,
        fUnDecimal2: undefined,
        fUnDecimal3: undefined,
        fUnDecimal4: undefined,
        fUnDecimal5: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMaterial(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物料基础信息";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateMaterial(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addMaterial(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    handleFerpclsid(row) {
      let str = '';

      switch(parseInt(row.ferpclsid, 10)) {
        case 1:
          str = '外购';
        break;
        case 2:
          str = '自制';
        break;
        case 3:
          str = '委外';
        break;
        case 9:
          str = '配置';
        break;
        case 10:
          str = '资产';
        break;
        case 11:
          str = '费用';
        break;
        case 5:
          str = '虚拟';
        break;
        case 6:
          str = '服务';
        break;
        case 13:
          str = '产品系列';
        break;
        default:
          str = '';
      }

      return str;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除物料基础信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMaterial(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有物料基础信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportMaterial(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
